/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */
package de.kieselbach.grootional

import de.kieselbach.swing.*
import de.kieselbach.grootional.component.*

import javax.swing.*

import java.awt.BorderLayout

public class DatabaseFrame {

    final static int RECENT_FILE_COUNT = 10

    def uiResources
    def resourceBundle

    def properties = [:]
    def recentFiles = [] as List

    def closeListener
    def connection
    def id

    def frame
    def splitPane

    def leftPane
    def mainPane

    def infoBrowser
    
    def doClose = {event ->
        close()
    }

    DatabaseFrame(def uires, def cl, def con) {

        closeListener = cl
        connection = con
        id = connection.connectionValue.id

        frame = new JFrame()
        frame.defaultCloseOperation = JFrame.DO_NOTHING_ON_CLOSE
        frame.windowClosing = doClose

        uiResources = uires

        frame.iconImage = uiResources.images['datasources16'].image
        resourceBundle = uiResources.resourceBundles['gui']

        frame.title = resourceBundle.getString('main.title') + ' - ' + connection.description

        def uiset = uiResources.createUISet('database')
        uiset.actions['close'] = doClose
        uiset.actions['metadatacard'] = {
            mainPane.addCard(new TableMetaDataCard(uires, connection, infoBrowser), true)
        }
        uiset.actions['sqlcard'] = {
            mainPane.addCard(new SqlEditorCard(this), true)
        }

        frame.setJMenuBar(uiset.menuBar)

        def main = new JPanel(new BorderLayout(0, 0))
        frame.contentPane = main

        main.add(uiset.toolBar as JToolBar, BorderLayout.NORTH)

        leftPane = new CardPane(frame, uiResources, false, 5, 5, 5, 1)
        infoBrowser = new InfoBrowserCard(uires, con)
        leftPane.addCard(infoBrowser)

        mainPane = new TabbedCardPane(frame, uiResources, true, 5, 1, 5, 5)
		
        splitPane = new CleanSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, leftPane, mainPane)
        main.add(splitPane, BorderLayout.CENTER)
    }

    void show(def props) {

        if(props != null) {
            props.each() {key,value ->
                properties[key] = value
            }
        }

        for(int i=0;i<RECENT_FILE_COUNT;i++) {
            def str = properties.remove('recentFile_' + i)
            if(str != null) {
                recentFiles.add(new File(str as String))
            }
        }

        def toInt = {str ->
            if (str != null && str.isInteger()) {
                return Integer.parseInt(str)
            }
            else {
                return null
            }
        }

        def x = toInt(properties['frameX'])
        def y = toInt(properties['frameY'])
        def w = toInt(properties['frameWidth'])
        def h = toInt(properties['frameHeight'])
        def divloc = toInt(properties['dividerLocation'])

        if(x != null && x >= 0 && y != null && y >= 0 && w != null && w > 0 && h != null && h > 0) {
            frame.size = [w, h]
            frame.location = [x, y]
        }
        else {
            frame.size = [600, 400]
            frame.location = [50, 50]
        }
        if(divloc != null && divloc > 0) {
            splitPane.dividerLocation = divloc
        }

        mainPane.addCard(new SqlEditorCard(this), true)

        frame.visible = true
    }

    void close() {

        properties['frameX'] = frame.location.@x as String
        properties['frameY'] = frame.location.@y as String
        properties['frameWidth'] = frame.width as String
        properties['frameHeight'] = frame.height as String
        properties['dividerLocation'] = splitPane.dividerLocation as String

        mainPane.cards.each() {card ->
            card.dispose()
        }

        leftPane.cards.each() {card ->
            card.dispose()
        }

        recentFiles.eachWithIndex() {file, i ->
            properties['recentFile_' + i] = file.toString()
        }

        if(closeListener != null) {
            closeListener(this)
        }

        connection.dispose()

        frame.visible = false
        frame.dispose()
    }

    def getDescription() {

        connection.description
    }

    void addRecentFile(File file) {

        recentFiles.remove(file)
        recentFiles.add(0, file)
        if(recentFiles.size() > RECENT_FILE_COUNT) {
            recentFiles.remove(recentFiles.size()-1);
        }
    }
}